//
//  ByteDanceAdsManager.m
//  HybidMediation
//
//  Created by jay Win on 2019/10/12.
//  Copyright © 2019 jay Win. All rights reserved.
//

#import "ByteDanceAdsManager.h"
@interface ByteDanceAdsManager()<BUFullscreenVideoAdDelegate,BURewardedVideoAdDelegate,BUSplashAdDelegate,BUNativeExpressBannerViewDelegate>
@property(nonatomic, strong) BUNativeExpressBannerView *bannerView;
@property(nonatomic,strong)BUFullscreenVideoAd *interstitial;
@property (nonatomic, strong) BURewardedVideoAd *rewardedVideoAd;
@property (nonatomic, strong) NSString *splash_id;
@property (strong, nonatomic) BUSplashAdView *splash;
@property (strong, nonatomic) UIView *customBottomView;

@end

@implementation ByteDanceAdsManager

#pragma mark   初始化
- (void)initAds:(void (^)(void))succeed{
    NSString *app_id=[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_app_id"];
    [self loadPlacementID];
    if(app_id){
        [BUAdSDKManager setAppID:app_id];
        if(self.debug){
            [BUAdSDKManager setLoglevel:BUAdSDKLogLevelDebug];
        }
        succeed();
        [self requestRewardVideo];
        [self requestInterstitial];
    }else{
        NSLog(@"==========尚未读取到头条应用id，广告初始化失败");
        return;
    }
}
- (void)loadPlacementID{    
    if(iPad&&[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_pad_banner_id"]){
        self.banner_id=[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_pad_banner_id"];
    }else{
        if([[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_banner_id"]==nil)
            NSLog(@"tt_banner_id为空，请在AdsConfig文件中配置tt_banner_id字段");
        self.banner_id=[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_banner_id"];
    }
    if(iPad&&[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_pad_interstitial_id"]){
        self.interstitial_id=[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_pad_interstitial_id"];
     }else{
         if([[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_interstitial_id"]==nil)
              NSLog(@"tt_interstitial_id为空，请在AdsConfig文件中配置tt_interstitial_id字段");
         self.interstitial_id=[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_interstitial_id"];
     }
    if(iPad&&[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_pad_rewardVideo_id"]){
        self.reward_id=[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_pad_rewardVideo_id"];
     }else{
         if([[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_rewardVideo_id"]==nil)
             NSLog(@"tt_rewardVideo_id为空，请在AdsConfig文件中配置tt_rewardVideo_id字段");
         self.reward_id=[[[KKADManager shareInstance]adsConfig]objectForKey:@"tt_rewardVideo_id"];
     }
}



- (void)agreeCOPPA{

    
}
- (void)agreeGDPR{
    
}

- (void)showlog:(NSObject *)obj{
    if(self.debug)
        NSLog(@"====ByteDance=====%@",obj);
}

#pragma mark   Banner
- (void)showBanner:(UIViewController *)rootViewController bannerContainner:(UIView *)containner{
    if (_bannerView == nil) {
        CGFloat screenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
        CGFloat bannerHeigh = screenWidth/600*90;
        BUSize *imgSize = [BUSize sizeBy:BUProposalSize_Banner600_150];
        _bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:self.banner_id rootViewController:rootViewController imgSize:imgSize adSize:CGSizeMake(screenWidth, bannerHeigh) IsSupportDeepLink:YES interval:30];
        if(IPhoneX){
            _bannerView.frame = CGRectMake((SCREENWIDTH -screenWidth) / 2,
                                           self.isBannerTop?44:SCREENHEIGHT - bannerHeigh-34,
                                           screenWidth,bannerHeigh);
        }else {
            _bannerView.frame = CGRectMake((SCREENWIDTH- screenWidth) / 2,
                                           self.isBannerTop?0:SCREENHEIGHT- bannerHeigh,
                                           screenWidth,bannerHeigh);
        }
        _bannerView.delegate = self;
        [containner addSubview:_bannerView];
    }
    [_bannerView loadAdData];
}
- (void)showBanner:(UIViewController *)rootViewController bannerContainner:(UIView *)containner isTop:(BOOL)position{
    self.needFixBannerSize=YES;
    self.isBannerTop=position;
    [self showBanner:rootViewController bannerContainner:containner];
}
- (void)showBanner:(UIViewController *)rootViewController bannerUIContainer:(UIView *)container onImpressionEvent:(void (^)(void))onImpression{
    self.onBannerImpression=onImpression;
    [self showBanner:rootViewController bannerContainner:container];
}
- (void)hideBanner{
    if(_bannerView){
        [_bannerView removeFromSuperview];
        _bannerView=nil;
    }
}
#pragma mark   Banner回调
//banner加载成功
- (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView{
    [self showlog:@"banner加载成功"];
    [self.delegate onBannerLoaded];
}
//banner加载失败
- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *)error{
    if(error){
         [self showlog:@"banner加载失败"];
         [self.delegate onBanenrLoadedFailed:error.localizedDescription];
    }
}
//banner点击
- (void)nativeExpressBannerAdViewDidClick:(BUNativeExpressBannerView *)bannerAdView{
    [self showlog:@"banner点击"];
    [self.delegate onBannerClick];
}
//banner渲染成功
- (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView{
    [self showlog:@"banner展示"];
    [self.delegate onBannerImpression];
}
//banner渲染失败
- (void)nativeExpressBannerAdViewRenderFail:(BUNativeExpressBannerView *)bannerAdView error:(NSError *)error{
    [self showlog:@"banner渲染失败"];
}
//banner已经显示成功
- (void)nativeExpressBannerAdViewWillBecomVisible:(BUNativeExpressBannerView *)bannerAdView{
    
}


#pragma mark   插屏
- (void)requestInterstitial{
   if(self.isRequestingInterstitial)
        return;
    [self showlog:@"请求插屏广告"];
    self.isRequestingInterstitial=YES;
    _interstitial=nil;
    _interstitial=[[BUFullscreenVideoAd alloc]initWithSlotID:self.interstitial_id];
    _interstitial.delegate=self;
    [_interstitial loadAdData];
    [self.delegate onInterstitialRequest];
}
- (BOOL)hasInterstitial{
    if(_interstitial!=nil&&_interstitial.adValid){
        return  YES;
    }else{
        [self requestInterstitial];
        return  NO;
    }
}

- (void)showInterstitial:(UIViewController *)rootViewController onOpenEvent:(void (^)(void))onOpen onCloseEvent:(void (^)(void))onClose{
    self.onVideoOpen =onOpen;
    self.onVideoClose=onClose;
    [self showInterstitial:rootViewController];
}
- (void)showInterstitial:(UIViewController *)rootViewController{
    if([self hasInterstitial]){
        [self.delegate onInterstitialTrigger];
        [_interstitial showAdFromRootViewController:rootViewController];
    }
}
#pragma mark   插屏回调
//物料加载成功
- (void)fullscreenVideoMaterialMetaAdDidLoad:(BUFullscreenVideoAd *)fullscreenVideoAd{
    [self showlog:@"插屏物料加载成功"];
}
//加载成功
- (void)fullscreenVideoAdVideoDataDidLoad:(BUFullscreenVideoAd *)fullscreenVideoAd{
    [self showlog:@"插屏加载成功"];
    self.isRequestingInterstitial=NO;
    [self.delegate onInterstitialLoaded];
}
//加载失败
- (void)fullscreenVideoAd:(BUFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *)error{
    self.isRequestingInterstitial=NO;
    if(error){
        [self showlog:@"插屏加载失败"];
        [self.delegate onInterstitialLoadedFailed:error.localizedDescription];
    }
}
//将要打开
- (void)fullscreenVideoAdWillVisible:(BUFullscreenVideoAd *)fullscreenVideoAd{
    
}
//已经打开
- (void)fullscreenVideoAdDidVisible:(BUFullscreenVideoAd *)fullscreenVideoAd{
    if(self.onVideoOpen)
        [self onVideoOpen];
    [self showlog:@"插屏已经打开"];
    [self.delegate onInterstitialOpen];
}
//跳过
- (void)fullscreenVideoAdDidClickSkip:(BUFullscreenVideoAd *)fullscreenVideoAd{
    
}
//播放失败
- (void)fullscreenVideoAdDidPlayFinish:(BUFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *)error{
    if(error){
       [self showlog:@"插屏播放失败"];
       [self.delegate onInterstitialPlayFailed:error.localizedDescription];
    }
}
//点击
- (void)fullscreenVideoAdDidClick:(BUFullscreenVideoAd *)fullscreenVideoAd{
    [self.delegate onInterstitialClick];
}
//将要关闭
- (void)fullscreenVideoAdWillClose:(BUFullscreenVideoAd *)fullscreenVideoAd{
    
}
//已经关闭
- (void)fullscreenVideoAdDidClose:(BUFullscreenVideoAd *)fullscreenVideoAd{
    [self showlog:@"插屏关闭"];
    if(self.onVideoClose)
        [self onVideoClose];
    [self.delegate onInterstitialClose];
    [self requestInterstitial];
}

#pragma mark   激励
- (void)requestRewardVideo{
    if(self.isRequestingRewardVideo)
        return;
    self.isRequestingRewardVideo=YES;
    [self showlog:@"请求激励视频广告"];
    _rewardedVideoAd=nil;
    _rewardedVideoAd=[[BURewardedVideoAd alloc]initWithSlotID:self.reward_id rewardedVideoModel:[[BURewardedVideoModel alloc]init]];
    _rewardedVideoAd.delegate=self;
    [_rewardedVideoAd loadAdData];
    [self.delegate onRewardVideoRequest];
}
- (BOOL)hasRewardVideo{
    if(_rewardedVideoAd!=nil&&_rewardedVideoAd.isAdValid){
        return  YES;
    }else{
        [self requestRewardVideo];
        return  NO;
    }
}

- (void)showRewardVideo:(UIViewController *)rootViewController{
    if([self hasRewardVideo]){
         [_rewardedVideoAd showAdFromRootViewController:rootViewController];
    }else{
        [self showlog:@"激励视频尚未准备好"];
    }
}
- (void)showRewardVideo:(UIViewController *)rootViewController onOpenEvent:(void (^)(void))onOpen onCloseEvent:(void (^)(void))onClose onCompleteEvent:(void (^)(void))onComplete{
    self.onVideoOpen=onOpen;
    self.onVideoClose=onClose;
    self.onVideoComplete=onComplete;
    [self showRewardVideo:rootViewController];
}
#pragma mark   激励回调
//加载成功
- (void)rewardedVideoAdDidLoad:(BURewardedVideoAd *)rewardedVideoAd{
    [self showlog:@"激励加载成功"];
    self.isRequestingRewardVideo=NO;
    [self.delegate onRewardVideoLoaded];
}
//加载失败
- (void)rewardedVideoAd:(BURewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    self.isRequestingRewardVideo=NO;
    if(error){
      [self showlog:@"激励加载失败"];
      [self.delegate onRewardVideoLoadedFailed:error.localizedDescription];
    }
}
//点击
- (void)rewardedVideoAdDidClick:(BURewardedVideoAd *)rewardedVideoAd{
    [self showlog:@"激励点击"];
    [self.delegate onRewardVideoClick];
}
//即将打开
- (void)rewardedVideoAdWillVisible:(BURewardedVideoAd *)rewardedVideoAd{
    
}
//已经打开
- (void)rewardedVideoAdDidVisible:(BURewardedVideoAd *)rewardedVideoAd{
    [self showlog:@"激励打开"];
    if(self.onVideoOpen)
        [self onVideoOpen];
    [self.delegate onRewardVideoOpen];
}
//即将关闭
- (void)rewardedVideoAdWillClose:(BURewardedVideoAd *)rewardedVideoAd{

}
//已经关闭
- (void)rewardedVideoAdDidClose:(BURewardedVideoAd *)rewardedVideoAd{
    if(self.onVideoComplete)
        [self onVideoComplete];
    [self.delegate onRewardVideoComplete];
    [self requestRewardVideo];
}
//跳过
- (void)rewardedVideoAdDidClickSkip:(BURewardedVideoAd *)rewardedVideoAd{
    
}
//播放错误
- (void)rewardedVideoAdDidPlayFinish:(BURewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    if(error){
        [self.delegate onRewardVideoPlayFailed:error.localizedDescription];
    }
}
//服务器验证成功
- (void)rewardedVideoAdServerRewardDidSucceed:(BURewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify{

}

@end
